package com.ztesoft.sca.util.variable;

import org.springframework.util.StringUtils;

/**
 * 驼峰和下划线互转
 * @author liang.aozhao
 * @created 2018/5/23 15:02
 */
public class NameRule {

    public static String UnderLine2Camel(String str) {
        StringBuffer sb = new StringBuffer(str.toLowerCase());
        while (sb.indexOf("_") != -1) {
            String upCase = String.valueOf(sb.charAt(sb.indexOf("_") + 1)).toUpperCase();
            sb.replace(sb.indexOf("_"), sb.indexOf("_") + 2, upCase);
        }
        return sb.toString();
    }


    public static String Camel2UnderLine(String name) {
        if (!StringUtils.hasLength(name)) {
            return "";
        } else {
            StringBuilder result = new StringBuilder();
            result.append((name.substring(0, 1)).toLowerCase());
            for (int i = 1; i < name.length(); ++i) {
                String s = name.substring(i, i + 1);
                String slc = s.toLowerCase();
                if (!s.equals(slc)) {
                    result.append("_").append(slc);
                } else {
                    result.append(s);
                }
            }
            return result.toString();
        }
    }
}
